//package com.china.produce.controller;
//
//import com.china.produce.annotation.ResponseResult;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.core.MethodParameter;
//import org.springframework.http.MediaType;
//import org.springframework.http.converter.HttpMessageConverter;
//import org.springframework.http.server.ServerHttpRequest;
//import org.springframework.http.server.ServerHttpResponse;
//import org.springframework.web.bind.annotation.ControllerAdvice;
//import org.springframework.web.context.request.RequestContextHolder;
//import org.springframework.web.context.request.ServletRequestAttributes;
//import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
//import javax.servlet.http.HttpServletRequest;
//
///**
// * @ClassName
// * @Author niujie
// * @Version
// * @Description
// * @CreateTime 2022/6/22
// */
//@ControllerAdvice
//public class ResponseResultHandler implements ResponseBodyAdvice<Object> {
//
//    public static final String RESPONSE_ANNOTATION = "RESPONSE_ANNOTATION";
//
//    private static final Logger logger = LoggerFactory.getLogger(ResponseResultHandler.class);
//
//    @Override
//    public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
//
//        ServletRequestAttributes ra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//        HttpServletRequest sr = (HttpServletRequest) ra.getRequest();
//
//        // 查询是否需要进行响应包装的标志
//        ResponseResult responseResult = (ResponseResult) sr.getAttribute(RESPONSE_ANNOTATION);
//
//        logger.info("result : {}", responseResult);
//
//        return responseResult == null ? false : true;
//    }
//
//    @Override
//    public Object beforeBodyWrite(Object responseBody, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
//
//        logger.info("返回响应 包装进行中。。。");
//
//        // boolean类型时判断一些数据库新增、更新、删除的操作是否成功
//        if (responseBody instanceof Boolean) {
//            if ((Boolean) responseBody) {
//                System.out.println("1111 !");
//            } else {
//                System.out.println("2222 !");
//            }
//        } else {
//
//            System.out.println("6666 !");
//        }
//
//        System.out.println("responseBody : " + responseBody);
//
//        return responseBody + " : res";
//    }
//}
